package com.soft.sitegenerator.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.soft.sitegenerator.Component;
import com.soft.sitegenerator.DataBaseComponent;
import com.soft.sitegenerator.business.hibernate.CompositeTemplate;
import com.soft.sitegenerator.business.hibernate.User;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;
import com.soft.sitegenerator.form.CompositeTemplateEditForm;



public class CompositeTemplateEditAction extends DispatchAction { 
	
	public ActionForward editCompositeTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
	
		CompositeTemplateEditForm compositeTemplateEditForm = (CompositeTemplateEditForm) form;

		Long id = new Long(Long.parseLong(request.getParameter("id")));

		CompositeTemplate compositeTemplate = compositeTemplateEditForm
				.getCompositeTemplate();
		compositeTemplate.setId(id);

		try {
			compositeTemplate.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		Component component = new DataBaseComponent();
		User user = (User) component.getBean("usersHib");
		compositeTemplateEditForm.setUsers(user.findAll());
		compositeTemplateEditForm.setIdOwner(compositeTemplate.getOwner().getId());
	
		return mapping.findForward("showEdit");
	}

	public ActionForward deleteCompositeTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
	CompositeTemplateEditForm compositeTemplateEditForm = (CompositeTemplateEditForm) form;

		Long id = new Long(Long.parseLong(request.getParameter("id")));

		compositeTemplateEditForm.getCompositeTemplate().setId(id);
		compositeTemplateEditForm.getCompositeTemplate().delete();

		return mapping.findForward("showList");
	}

		
	public ActionForward addCompositeTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		CompositeTemplateEditForm compositeTemplateEditForm = (CompositeTemplateEditForm) form;

		Component component = new DataBaseComponent();
		User user = (User) component.getBean("usersHib");
		compositeTemplateEditForm.setUsers(user.findAll());

		CompositeTemplate compositeTemplate = (CompositeTemplate) component.getBean("compositeTemplatesHib");
		compositeTemplateEditForm.setAllTemplates(compositeTemplate.findAll());

		return mapping.findForward("showAdd");
	}

	public ActionForward saveCompositeTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		

		CompositeTemplateEditForm compositeTemplateEditForm = (CompositeTemplateEditForm) form;

		Component component = new DataBaseComponent();
		//	levantar los sub componentes de la base
		
		User owner = (User) component.getBean("usersHib");
		owner.setId(compositeTemplateEditForm.getIdOwner());
		try {
			owner.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		CompositeTemplate compositeTemplate = compositeTemplateEditForm.getCompositeTemplate();
	
		//guardar todos los seleccionados
	
		compositeTemplate.setOwner(owner);
		if (compositeTemplate.getCanUpdate() == null)
			compositeTemplate.setCanUpdate("no");
		
		compositeTemplate.save();

		return mapping.findForward("showList");

	}

	public ActionForward updateCompositeTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
	
		
		CompositeTemplateEditForm compositeTemplateEditForm = (CompositeTemplateEditForm) form;

		Component component = new DataBaseComponent();
	
		//		levantar los sub componentes de la base
		
		
		
		User owner = (User) component.getBean("usersHib");
		owner.setId(compositeTemplateEditForm.getIdOwner());
		try {
			owner.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		CompositeTemplate compositeTemplate = compositeTemplateEditForm.getCompositeTemplate();
		
		//guardar todos los subcomponentes seleccionados
		
		compositeTemplate.setOwner(owner);
		if (compositeTemplate.getCanUpdate() == null)
			compositeTemplate.setCanUpdate("no");
		compositeTemplate.update();

		return mapping.findForward("showList");

	}


}
